package com.mcarthur.countdown.client;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.googlecode.gwtgl.binding.WebGLCanvas;
import com.googlecode.gwtgl.binding.WebGLRenderingContext;
import com.mcarthur.countdown.client.drawable.BlackHoleAnimator;
import com.mcarthur.countdown.client.drawable.BouncyBallManager;
import com.mcarthur.countdown.client.drawable.DefaultAnimator;
import com.mcarthur.countdown.client.renderer.CountdownRenderer;

public class GWTCountdown implements EntryPoint {

    private CountdownRenderer countdownRenderer;
    private List<String> options = Arrays.asList("Classic", "Black Hole");

    public void onModuleLoad() {
        addOptions();
        
        
        WebGLCanvas webGLCanvas = new WebGLCanvas("500px", "500px");
        WebGLRenderingContext glContext = webGLCanvas.getGlContext();
        glContext.viewport(0, 0, 500, 500);
        RootPanel.get("gwtGL").add(webGLCanvas);
        
        countdownRenderer = new CountdownRenderer(webGLCanvas, glContext);
        countdownRenderer.start();
    }
    
    private void addOptions() {
        final ListBox box = new ListBox();
        box.addChangeHandler(new ChangeHandler() {
            
            @Override
            public void onChange(ChangeEvent event) {
                String selected = box.getItemText(box.getSelectedIndex());
                if (selected.equals("Classic")) {
                    BouncyBallManager.get().setAnimator(new DefaultAnimator());
                } else if (selected.equals("Black Hole")) {
                    BouncyBallManager.get().setAnimator(new BlackHoleAnimator());
                }
            }
        });
        box.getElement().setId("options");
        for (String s : options) {
            box.addItem(s);
        }
        RootPanel.get().add(box);
    }
}
